
/* JavaScript content from common/js/base.js in Common Resources */
/* Copyright © Worklight Ltd. 2006-2011.  All rights reserved. */

function __WL() {
};
var WL = WL ? WL : {};

window.WLJSX = {
    /*
     * Search for an element with a specified Id and returns it as a DOM element. Returns null if element is
     * not found @Param selector - a string with the requires DOM element's Id
     */
    $ : function(selector) {
	var elements = WLJQ("#" + selector);
	if (elements.length == 0) {
	    return null;
	} else {
	    return elements[0];
	}
    },

    /*
     * Searches for the elements with a specified selector and returns them as an array of DOM elements @Param
     * selector - a string representing a CSS selector
     */
    $$ : function(selector) {
	return WLJQ(selector);
    },

    /*
     * Same as $$ but searches inside of a given element only. Returns array of DOM elements @Param el - the
     * DOM element to search inside of @Param selector - a string representing a CSS selector
     */
    find : function(el, selector) {
	return WLJQ(el).find(selector);
    },

    /*
     * Creates a new DOM element and returns it @Param type - a string representing the element type (tag
     * name, e.g. "<div/>") @Param attrs - an object of attributes to be added to newly created element
     */
    newElement : function(type, attrs) {
	return WLJQ(type, attrs)[0];
    },

    /*
     * Appends the content before the end of a DOM element @Param el - the DOM element (or CSS selector
     * string) to append content to @Param content - new content to be appended
     */
    append : function(el, content) {
	WLJQ(el).append(content);
    },

    /*
     * Prepends the content at the beginning of a DOM element @Param el - the DOM element (or CSS selector
     * string) to prepend content to @Param content - new content to be prepended
     */
    prepend : function(el, content) {
	WLJQ(el).prepend(content);
    },

    /*
     * Sets or Gets DOM element's content @Param el - the DOM element to update content in @Param content -
     * new content, can be string or other DOM elements
     */
    html : function(el, content) {
	if (content) {
	    WLJQ(el).html(content);
	} else {
	    return WLJQ(el).html();
	}
    },

    /*
     * Empties the content of a given DOM element @Param el - the DOM element (or CSS selector string) to
     * empty
     */
    empty : function(el) {
	WLJQ(el).empty();
    },

    /*
     * Shows a DOM element (makes visible) @Param el - the DOM element (or CSS selector string) to make
     * visible
     */
    show : function(el) {
	WLJQ(el).show();
    },

    /*
     * Hides a DOM element (makes invisible) @Param el - the DOM element (or CSS selector string) to hide
     */
    hide : function(el) {
	WLJQ(el).hide();
    },

    /*
     * Adds a specified CSS class to DOM elements @Param el - the DOM element (or CSS selector string) to add
     * the CSS class to @Param className - string with the class' name
     */
    addClass : function(el, className) {
	WLJQ(el).addClass(className);
    },

    /*
     * Removes a specified CSS class from DOM elements @Param el - the DOM element (or CSS selector string) to
     * remove the CSS class from @Param className - string with the class' name
     */
    removeClass : function(el, className) {
	WLJQ(el).removeClass(className);
    },

    /*
     * Sets or Gets the width of a DOM element (first one in case several elements fit CSS selector) @Param el -
     * the DOM element to get/set width @Param width - new width to set
     */
    width : function(el, width) {
	if (width) {
	    WLJQ(el).width(width);
	} else {
	    return WLJQ(el).width();
	}
    },

    /*
     * Sets or Gets the height of a DOM element (first one in case several elements fit CSS selector) @Param
     * el - the DOM element to get/set height @Param height - new height to set
     */
    height : function(el, height) {
	if (height) {
	    WLJQ(el).height(height);
	} else {
	    return WLJQ(el).height();
	}
    },

    /*
     * Removes an element from the DOM. @Param el - the DOM element (or CSS selector string) to remove
     */
    remove : function(el) {
	WLJQ(el).remove();
    },

    /*
     * Sets specific CSS style on the DOM elements @Param el - the DOM element (or CSS selector string) to set
     * CSS style on @Param style - an object of CSS styles to be set
     */
    css : function(el, style) {
	WLJQ(el).css(style);
    },

    /*
     * Sets or Gets the attribute of a DOM element @Param el - the DOM element to get/set attribute @Param
     * attrName - the name of an attribute @Param attrValue - the new value of the attribute
     */
    attr : function(el, attrName, attrValue) {
	if (attrValue) {
	    WLJQ(el).attr(attrName, attrValue);
	} else {
	    return WLJQ(el).attr(attrName);
	}
    },

    /*
     * Adds the event listener to DOM elements for a specified event @Param el - the DOM element (or CSS
     * selector string) to add event listener to @Param event - string with the event's name, e.g. "click",
     * "change" etc. @Param callback - a JavaScript function to be invoked once event is triggered
     */
    bind : function(el, event, callback) {
	WLJQ(el).bind(event, callback);
    },

    /*
     * Removes the event listener from DOM elements @Param el - the DOM element (or CSS selector string) to
     * remove event listener form @Param event - string with the event's name, e.g. "click", "change" etc.
     */
    unbind : function(el, event) {
	if (event) {
	    WLJQ(el).unbind(event);
	} else {
	    WLJQ(el).unbind();
	}
    },

    /*
     * Triggers a specific event on DOM elements @Param el - the DOM element (or CSS selector string) to
     * trigger the event on @Param event - string with the event's name, e.g. "click", "change" etc.
     */
    trigger : function(el, event) {
	WLJQ(el).trigger(event);
    },

    /*
     * Retrieves the element that triggered the event (event's target) @Param event - event to get the target
     * from
     */
    eventTarget : function(event) {
	return event.target;
    }

};
